using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

// ****************************************************************************** //
//         This component handles the input for the gameplay of the Game          //
// ****************************************************************************** //

// ****************************************************************************** //
//   NOTE: to be able to decouple keyboard/pad keys to game actions it has been   //
//   necessary to create a new project for the input only, in order to reference  //
//    this solution both in the Csharp classes of the game and the Fsharp ones.   //
// ****************************************************************************** //

namespace Frogger_Input
{
    /// <summary>
    /// Component that implements IUpdateable.
    /// </summary>
    public class InputHandler : Microsoft.Xna.Framework.GameComponent
    {
        public InputHandler(Game game)
            : base(game)
        {
        }

        public struct InputState
        {
            public bool left;
            public bool right;
            public bool up;
            public bool down;

            public KeyboardState oldState;
            public GamePadState oldGState;
        }


        private static InputState inputState;

        public static InputState GetState()
        {
        return inputState;
        }

        /// <summary>
        /// Initialize variables
        /// </summary>
        public override void Initialize()
        {
            inputState.left = false;
            inputState.right = false;
            inputState.up = false;
            inputState.down = false;
            inputState.oldState = Keyboard.GetState();
            inputState.oldGState = GamePad.GetState(PlayerIndex.One);
            base.Initialize();
        }

        /// <summary>
        /// Update method
        /// </summary>
        public override void Update(GameTime gameTime)
        {
            
#if XBOX   
            inputState.left = GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.LeftThumbstickLeft) && 
                inputState.oldGState.IsButtonUp(Buttons.LeftThumbstickLeft) && 
                GamePad.GetState(PlayerIndex.One).IsButtonUp(Buttons.LeftThumbstickRight) && 
                GamePad.GetState(PlayerIndex.One).IsButtonUp(Buttons.LeftThumbstickDown) && 
                GamePad.GetState(PlayerIndex.One).IsButtonUp(Buttons.LeftThumbstickUp);

            inputState.right = GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.LeftThumbstickRight) && 
                inputState.oldGState.IsButtonUp(Buttons.LeftThumbstickRight) && 
                GamePad.GetState(PlayerIndex.One).IsButtonUp(Buttons.LeftThumbstickLeft)  && 
                GamePad.GetState(PlayerIndex.One).IsButtonUp(Buttons.LeftThumbstickDown) && 
                GamePad.GetState(PlayerIndex.One).IsButtonUp(Buttons.LeftThumbstickUp);

            inputState.up = GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.LeftThumbstickUp) && 
                inputState.oldGState.IsButtonUp(Buttons.LeftThumbstickUp) && 
                GamePad.GetState(PlayerIndex.One).IsButtonUp(Buttons.LeftThumbstickLeft)  && 
                GamePad.GetState(PlayerIndex.One).IsButtonUp(Buttons.LeftThumbstickDown) && 
                GamePad.GetState(PlayerIndex.One).IsButtonUp(Buttons.LeftThumbstickRight);

            inputState.down = GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.LeftThumbstickDown) && 
                inputState.oldGState.IsButtonUp(Buttons.LeftThumbstickDown) && 
                GamePad.GetState(PlayerIndex.One).IsButtonUp(Buttons.LeftThumbstickLeft)  &&
                GamePad.GetState(PlayerIndex.One).IsButtonUp(Buttons.LeftThumbstickUp) && 
                GamePad.GetState(PlayerIndex.One).IsButtonUp(Buttons.LeftThumbstickRight);
            
            //if we want to use DPAD instead of Analogue Stick.
            //inputState.left = GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.DPadLeft) && inputState.oldGState.IsButtonUp(Buttons.DPadLeft) && GamePad.GetState(PlayerIndex.One).IsButtonUp(Buttons.DPadRight) && GamePad.GetState(PlayerIndex.One).IsButtonUp(Buttons.DPadDown) && GamePad.GetState(PlayerIndex.One).IsButtonUp(Buttons.DPadUp);
            //inputState.right = GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.DPadRight) && inputState.oldGState.IsButtonUp(Buttons.DPadRight) && GamePad.GetState(PlayerIndex.One).IsButtonUp(Buttons.DPadLeft) && GamePad.GetState(PlayerIndex.One).IsButtonUp(Buttons.DPadDown) && GamePad.GetState(PlayerIndex.One).IsButtonUp(Buttons.DPadUp);
            //inputState.up = GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.DPadUp) && inputState.oldGState.IsButtonUp(Buttons.DPadUp) && GamePad.GetState(PlayerIndex.One).IsButtonUp(Buttons.DPadLeft) && GamePad.GetState(PlayerIndex.One).IsButtonUp(Buttons.DPadDown) && GamePad.GetState(PlayerIndex.One).IsButtonUp(Buttons.DPadRight);
            //inputState.down = GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.DPadDown) && inputState.oldGState.IsButtonUp(Buttons.DPadDown) && GamePad.GetState(PlayerIndex.One).IsButtonUp(Buttons.DPadLeft) && GamePad.GetState(PlayerIndex.One).IsButtonUp(Buttons.DPadUp) && GamePad.GetState(PlayerIndex.One).IsButtonUp(Buttons.DPadRight);

            inputState.oldGState = GamePad.GetState(PlayerIndex.One);
#else
            inputState.left = Keyboard.GetState().IsKeyDown(Keys.Left) && 
                inputState.oldState.IsKeyUp(Keys.Left) && 
                Keyboard.GetState().IsKeyUp(Keys.Right) && 
                Keyboard.GetState().IsKeyUp(Keys.Down) && 
                Keyboard.GetState().IsKeyUp(Keys.Up);

            inputState.right = Keyboard.GetState().IsKeyDown(Keys.Right) && 
                inputState.oldState.IsKeyUp(Keys.Right) && 
                Keyboard.GetState().IsKeyUp(Keys.Left) && 
                Keyboard.GetState().IsKeyUp(Keys.Down) && 
                Keyboard.GetState().IsKeyUp(Keys.Up);

            inputState.up = Keyboard.GetState().IsKeyDown(Keys.Up) && 
                inputState.oldState.IsKeyUp(Keys.Up) && 
                Keyboard.GetState().IsKeyUp(Keys.Left) && 
                Keyboard.GetState().IsKeyUp(Keys.Down) && 
                Keyboard.GetState().IsKeyUp(Keys.Right);

            inputState.down = Keyboard.GetState().IsKeyDown(Keys.Down) && 
                inputState.oldState.IsKeyUp(Keys.Down) && 
                Keyboard.GetState().IsKeyUp(Keys.Left) && 
                Keyboard.GetState().IsKeyUp(Keys.Up) && 
                Keyboard.GetState().IsKeyUp(Keys.Right);

            inputState.oldState = Keyboard.GetState();
#endif     
            base.Update(gameTime);
        }
    }
}
